package demo

import (
	"fmt"

	request2 "github.com/flipped-aurora/gin-vue-admin/server/model/demo/request"

	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"

	"github.com/bwmarrin/snowflake"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/demo"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type SysTeacherApi struct {
}

var sysTeacherService = service.ServiceGroupApp.DemoServiceGroup.SysTeacherService

func (sysTeacherApi *SysTeacherApi) CreateSysTeacher(ctx *gin.Context) {
	var sysTeacher demo.SystemTeacher
	err := ctx.ShouldBindJSON(&sysTeacher)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}
	node, _ := snowflake.NewNode(1)
	var id = node.Generate().Int64()
	sysTeacher.ID = uint(id)

	fmt.Println("当前id为", sysTeacher.ID)
	if err := sysTeacherService.CreateTeacher(&sysTeacher); err != nil {
		global.GVA_LOG.Error("创建失败", zap.Error(err))
		response.FailWithMessage("保存老师信息报错啦"+err.Error(), ctx)
	} else {
		response.OkWithMessage("创建成功", ctx)
	}
}

func (sysTeacherApi *SysTeacherApi) DeleteSysTeacher(ctx *gin.Context) {
	var sysTeacher demo.SystemTeacher
	err := ctx.ShouldBindJSON(&sysTeacher)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}
	if err := sysTeacherService.DeleteSysTeacher(&sysTeacher); err != nil {
		global.GVA_LOG.Error("sysTeacher失败", zap.Error(err))
		response.FailWithMessage("删除老师信息报错啦"+err.Error(), ctx)
	} else {
		response.OkWithMessage("删除成功", ctx)
	}
}

//	DeleteSysTeacherByIds {
//	   "ids":[1651853941740146688]
//	}
func (sysTeacherApi *SysTeacherApi) DeleteSysTeacherByIds(ctx *gin.Context) {
	var Ids request.IdsReq
	err := ctx.ShouldBindJSON(&Ids)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}
	if err := sysTeacherService.DeleteSysTeacherByIds(&Ids); err != nil {
		global.GVA_LOG.Error("sysTeacher失败", zap.Error(err))
		response.FailWithMessage("删除老师信息报错啦"+err.Error(), ctx)
	} else {
		response.OkWithMessage("删除成功", ctx)
	}
}

func (sysTeacherApi *SysTeacherApi) UpdateSysTeacher(ctx *gin.Context) {
	var sysTeacher demo.SystemTeacher
	err := ctx.ShouldBindJSON(&sysTeacher)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}
	if err := sysTeacherService.UpdateSysTeacher(&sysTeacher); err != nil {
		global.GVA_LOG.Error("更新失败", zap.Error(err))
		response.FailWithMessage("更新老师信息报错啦"+err.Error(), ctx)
	} else {
		response.OkWithMessage("更新成功", ctx)
	}
}

func (sysTeacherApi *SysTeacherApi) GetSysTeacherById(ctx *gin.Context) {
	var sysTeacher demo.SystemTeacher
	err := ctx.ShouldBindJSON(&sysTeacher)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}
	if sysTeacher2, err := sysTeacherService.GetSysTeacherById(sysTeacher.ID); err != nil {
		global.GVA_LOG.Error("获取失败", zap.Error(err))
		response.FailWithMessage("获取老师信息报错啦"+err.Error(), ctx)
	} else {
		response.OkWithData(gin.H{"sysTeacher": sysTeacher2, "origin": sysTeacher}, ctx)
	}
}

func (sysTeacherApi *SysTeacherApi) ListSysTeacher(ctx *gin.Context) {
	var param request2.SysTeacherParam
	err := ctx.ShouldBindJSON(&param)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}
	if list, total, err := sysTeacherService.ListSysTeacher(param); err != nil {
		global.GVA_LOG.Error("获取失败", zap.Error(err))
		response.FailWithMessage("获取老师信息报错啦"+err.Error(), ctx)
	} else {
		response.OkWithData(gin.H{"list": list, "total": total}, ctx)
	}
}
